package com.tczs.web.service;

import com.alibaba.fastjson.JSONObject;
import com.tczs.client.Client;
import com.tczs.client.bean.Command;
import com.tczs.client.service.ClientService;

public class TableService {

    /**
     * 查询数据库下所有表
     * @param databaseName
     * @return
     */
    public JSONObject findTables(String databaseName){
        Command command = new Command();
        command.setCommandName("showTables");
        command.setDatabaseName(databaseName);
        String result = ClientService.send(null, command);
        return JSONObject.parseObject(result);
    }


    /**
     * 查询表的字段
     * @param databaseName
     * @param tableName
     * @return
     */
    public JSONObject findTableField(String databaseName,String tableName){
        Command command = new Command();
        command.setCommandName("showFields");
        command.setDatabaseName(databaseName);
        command.setTableName(tableName);
        String send = ClientService.send(null, command);
        return JSONObject.parseObject(send);
    }

    /**
     * 创建表
     * @param databaseName
     * @param tableName
     * @param commandData
     * @return
     */
    public JSONObject createTable(String databaseName,String tableName,String commandData){
        Command command = new Command();
        command.setCommandName("createTable");
        command.setDatabaseName(databaseName);
        command.setTableName(tableName);
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(command);
        jsonObject.put("commandData",commandData);
        String send = ClientService.send(null, jsonObject);
        return JSONObject.parseObject(send);
    }
}
